package com.ftg.learn.mall.mapper;

import com.ftg.learn.mall.dto.PmsProductResult;
import com.ftg.learn.mall.entity.PmsProduct;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 * 商品信息 Mapper 接口
 * </p>
 *
 * @author mallCommon
 * @since 2022-04-19
 */
public interface PmsProductMapper extends BaseMapper<PmsProduct> {

//    /**
//     * 根据商品id获取商品编辑信息
//     *
//     * @param pid
//     * @return
//     */
//    @Select("SELECT *,\n" +
//            " pc.parent_id cateParentId from pms_product p " +
//            " LEFT JOIN pms_product_category pc on pc.product_category_id = p.product_category_id" +
//            " WHERE p.product_id= #{pid};")
//    @Results({
//            @Result(property = "productId",column = "product_id"),
//            @Result(property = "productName",column = "product_name"),
//            @Result(property = "cateParentId",column = "cateParentId"),
//            @Result(property = "productAttributeValueList",column = "product_id",javaType = List.class,
//                    many = @Many(select = "com.ftg.learn.mall.mapper.PmsProductAttributeValueMapper.findProductAttributeValue")),
//            @Result(property = "skuStockList",column = "product_id",javaType = List.class,
//                    many = @Many(select = "com.ftg.learn.mall.mapper.PmsSkuStockMapper.findSkuStock"))
//
//    })
//    PmsProductResult getUpdateInfo(@Param("pid") Long pid);


    /**
     * 根据商品id获取商品编辑信息
     *
     * @param pid
     * @return
     */
    @Select("SELECT *\n" +
            " from pms_product p " +
            " WHERE p.product_id= #{pid};")
    @Results({
            @Result(property = "cateParentId",column = "product_id",javaType = Long.class,
                    one = @One(select = "getParentId")),
            @Result(property = "productAttributeValueList",column = "product_id",javaType = List.class,
                    many = @Many(select = "com.ftg.learn.mall.mapper.PmsProductAttributeValueMapper.findProductAttributeValue")),
            @Result(property = "skuStockList",column = "product_id",javaType = List.class,
                    many = @Many(select = "com.ftg.learn.mall.mapper.PmsSkuStockMapper.findSkuStock"))

    })
    PmsProductResult getUpdateInfo(@Param("pid") Long pid);


    /**
     * 根据pid查询出此商品所属于的产品类型的父级ID
     * @param pid
     * @return
     */
    @Select("select parent_id parentId from pms_product_category a left join pms_product b on a.product_category_id = " +
            "b.product_category_id where b.product_id = #{pid}")
    Long getParentId(@Param("pid") Long pid);

    /**
     * 批量修改审核状态
     * @param id
     * @return
     */
    @Update("UPDATE pms_product SET publish_status = #{publishStatus} WHERE product_id = #{id}")
    int updateVerifyStatus(@Param("id") Long id,@Param("publishStatus") Long publishStatus);

    /**
     * 批量上下架商品
     * @param id
     * @return
     */
    @Update("UPDATE pms_product SET recommand_status = #{recommendStatus} WHERE product_id = #{id}")
    int updatePublishStatus(@Param("id") Long id,@Param("recommendStatus") Long recommendStatus);


    /**
     * 批量修改删除状态
     * @param id
     * @return
     */
    @Update("UPDATE pms_product SET delete_status = #{deleteStatus} WHERE product_id = #{id}")
    int updateDeleteStatus(@Param("id") Long id,@Param("deleteStatus") Long deleteStatus);

}
